﻿$.ajaxSettings.async = false;//必须要同步读取商品资料，否则会导致合计之类的运算错误

//搜索商品
$("#InputCode").keypress(function (e) {
    if (e.keyCode == 13) {
        LoadProduct($("#InputCode").val(), null, null);
    }
});

$("#PrintOrder").click(function () { 
    window.open("../ReportPrint/" + $("#Id").val() + "?flag=" + $("#_jxcFlag").val(),"_blank");
}); 

$("body").on("click", ".btn-danger", function () {
    var obj = $(this).parent();
    var tdSeq = obj.parent().parent().find("tr").index(obj.parent()[0]);
    RemoveTableTr(tdSeq);
    CalcSum();
}); 
 
$("body").on("change", ".calcXj", function () {
    //小计
    var obj = $(this).parent().parent();// 行
    CalcSumX(obj);
    CalcSum();
}); 

function FormatProductList() {
    var sumAll = '';
    $("#tableDetail tr").each(function () {
        if (isNaN($(this).children("td:eq(0)").children().val()) == false) {
            var s1 = $(this).children("td:eq(0)").children().val() + ":";
            var s2 = $(this).children("td:eq(4)").children().val() + ":";
            var s3 = $(this).children("td:eq(5)").children().val() + ";";
            sumAll += (s1 + s2 + s3);
        }
    });
    return sumAll;
}

function FormatProductListString(str )
{
    var vr = (str + '').split(";"); 
    $.each(vr, function (i, item) {
        if (item.length > 3)
        {
            var v = item.split(":");
            LoadProduct(v[0], v[1], v[2] );
            CalcSumX($("#tableDetail tr:eq(1)")); 
        }
    });
    CalcSum();
}

function CalcSumX(tr)
{
    var one = tr.children("td:eq(4)").children().val();
    var two = tr.children("td:eq(5)").children().val(); 
    if (one < 0) {
        one = 0;
        tr.children("td:eq(4)").children().val("0")
    }
    if (two < 0) {
        two = 1;
        tr.children("td:eq(5)").children().val("1")
    }
    tr.children("td:eq(6)").text((one * two).toFixed(2));
}

function CalcSum() {
    var sumAll = 0;
    $("#tableDetail tr").each(function () {
        var sumValue = $(this).children("td:eq(6)").text();
        sumAll += Number(sumValue);
    });
    $("#calcSum").text(sumAll.toFixed(2));
}

//查找商品编码或条形码
function LoadProduct(code, price, count ) { 
    $.get("/Basic/GetProductJson", {
        code: code 
    },
        function (data) {
        var id = data["Id"];
        if (id == -1) {
            alert("找不到指定商品");
        }
        else {
            var pri = 0;
            var orderType = $("#_OrderType").val();
            if (price == null) { 
                if (orderType == "j") {
                    pri = data["PurchasePrice"];
                }
                else if(orderType== "x") {
                    pri = data["SalePrice"];
                }
                else {
                    pri = data["StorePrice"];
                }
            }
             
            var tr ='<tr><td> <input type="hidden" value='+data["Id"]+' />' + data["Code"] +'</td>';
            tr += ("<td>" + data["Barcode"] + "</td>");
            tr += ("<td>" + data["Name"] + "</td>");
            tr += ("<td>" + data["Unit"]["Name"] + "</td>");
            if (orderType== "x") {
                tr += ('<td><input type="number" min="0.01" class="calcXj" ' + ($("#ModifySellingPrice").val() == "True" ? '':'disabled="disabled"') + ' value="' + (price == null ? pri : price) + '" /></td>');
            }
            else if (orderType == "j")
            {
                tr += ('<td><input type="number" min="0.01" class="calcXj"  value="' + (price == null ? pri : price) + '" /></td>');
            }
            else {
                tr += ('<td><input type="number" min="0.01" class="calcXj" disabled="disabled"  value="' + (price == null ? pri : price) + '" /></td>');
            }
            tr += ('<td><input type="number" min="0"  class="calcXj" value="'+(count==null?1:count)+'" /></td>');
            tr += ('<td>' + pri + '</td>'); 
            tr += ('<td><button type="button" class="btn btn-danger">删除</button></td></tr>');
            AddTableTr(tr);
            $("#InputCode").val("");
            CalcSum();
        }
    });
}
 
function AddTableTr( trHtml) {
    //获取table最后一行 $("#tab tr:last")
    //获取table第一行 $("#tab tr").eq(0)
    //获取table倒数第二行 $("#tab tr").eq(-2)
    var $tr = $( "#tableDetail tr:first");
    if ($tr.size() == 0) {
        alert("指定的table id或行数不存在！");
        return;
    }
    $tr.after(trHtml);
}

function RemoveTableTr( rowIndex)
{
    var $tr = $("#tableDetail tr").eq(rowIndex);
    if ($tr.size() == 0) {
        alert("指定的table id或行数不存在！");
        return;
    }
    $tr.remove();
}
 
function DisabledControls(yes)
{
    if (yes== true) {
        $("#Advance").attr("disabled", "disabled");
        $("#Advance").val(0);
        $("#InvoiceType").attr("disabled", "disabled");
        $("#InvoiceType").val(0);
    }
    else
    {
        $("#Advance").removeAttr("disabled");  
        $("#InvoiceType").removeAttr("disabled");  
    }
}

$("#LoadProduct").click(function () {
    var pForm = window.open("/Basic/GetProductCategoryList?type=" + $("#_OrderType").val(), null, " height=600,width=800,Left=300px,Top=20px, menubar=no,titlebar=no,scrollbar=yes,toolbar=no, status=no,location=no");
    if (pForm) {
        window.pForm.focus();   
    } 
}); 

function GetProduct(code)
{
    LoadProduct(code, null, null);
}